﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_ControlMatHang : System.Web.UI.UserControl
{
    #region global variables
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = DRMConstants.EMPTY_STRING;
    #endregion


    #region protected methods
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW] == null)
            {
                ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW] = false;
            }
            bindDropDownLists(); 
        }
    }

    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW] = true;
        clearDetail();
    }

    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        int i = 0;

        try
        {
            if ((bool)ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW])
            {
                //insert
                insertMatHang();
                ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW] = false;
                clearDetail();
            }
            else
            {
                //update
                i = dt.ExecuteSQl_int(str, DRMConstants.STOREPROC_NAME_SAVE_MAST, CommandType.StoredProcedure, ref error,
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_MAST_ID, txtMaSo.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_DESC, txtDescription.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_BARCODE, txtExtendedCode.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_UNIT, txtUnit.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_VAT_RATE, ddlVAT.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_CATEGORY_ID, ddlVAT.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_BRAND_ID, ddlNhanHieu.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_PRODUCT, ddlVAT.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_PRICE, DRMConstants.KEY_WORD_NEW),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_STATUS, ddlStatus.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW, 1));
                if (i == 1)
                {
                    uscMsgBox1.AddMessage(String.Format(DRMConstants.MESSAGE_UPDATE_SUCCESSFULL, DRMConstants.OBJECT_NAME_CATEGORY),
                        uscMsgBox.enmMessageType.Success);
                }
                else
                {
                    uscMsgBox1.AddMessage(DRMConstants.MESSAGE_UNDER_CONSTRUCTION, uscMsgBox.enmMessageType.Error);
                }
            }
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }

    protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e)
    {

    }

    protected void imgBtnFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }

    protected void grvMatHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvMatHang.PageIndex = e.NewPageIndex;
        LoadGrid();
    }

    protected void grvMatHang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (e.CommandName == DRMConstants.COMMAND_NAME_ROW_COMMAND)
            {
                GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                Label lblMaSo = (Label)grv.FindControl(DRMConstants.CONTROL_NAME_LABEL_MASO);

                DataRow detailMatHang = getDetailMatHang(lblMaSo.Text.Trim());
                fillDetail(detailMatHang);

                grvMatHang.DataSource = null;
                grvMatHang.DataBind();

                ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW] = false;
                TabContainer1.ActiveTabIndex = 0;
            }
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        LoadGrid();
    }
    #endregion


    #region private methods
    private void clearDetail()
    {
        txtMaSo.Text = DRMConstants.EMPTY_STRING;
        txtMaSo.Enabled = true;
        txtDescription.Text = DRMConstants.EMPTY_STRING;
        txtMaSo1.Text = DRMConstants.EMPTY_STRING;
        txtDescription1.Text = DRMConstants.EMPTY_STRING; ;
        txtExtendedCode.Text = DRMConstants.EMPTY_STRING; ;
        txtUnit.Text = DRMConstants.EMPTY_STRING;
        ddlVAT.SelectedIndex = 0;
        ddlNganhHang.SelectedIndex = 0;
        ddlNhanHieu.SelectedIndex = 0;
        ddlProduct.SelectedIndex = 0;
        txtPrice.Text = DRMConstants.EMPTY_STRING;
        txtPV.Text = DRMConstants.EMPTY_STRING;
        txtBV.Text = DRMConstants.EMPTY_STRING; ;
        ddlStatus.SelectedIndex = 0;
    }

    private void unBindDropDownLists()
    {
        DRMCommonUtils.unBindDropDownList(ddlNganhHang);
        DRMCommonUtils.unBindDropDownList(ddlNhanHieu);
        DRMCommonUtils.unBindDropDownList(ddlProduct);
        DRMCommonUtils.unBindDropDownList(ddlStatus);
        DRMCommonUtils.unBindDropDownList(ddlVAT);
    }

    private void bindDropDownLists()
    {
        DRMCommonUtils.LoadNganhHangDropDowList(ddlNganhHang, getLogURL());
        DRMCommonUtils.LoadNhanHieuDropDowList(ddlNhanHieu, getLogURL());
        DRMCommonUtils.LoadVATDropDowList(ddlVAT, getLogURL());
        DRMCommonUtils.LoadProductDropDowList(ddlProduct, getLogURL());
        DRMCommonUtils.LoadStatusDropDowList(ddlStatus, getLogURL());
    }

    private void fillDetail(DataRow detailMatHang)
    {
        txtMaSo.Text = detailMatHang[DRMConstants.DB_COLUMN_MAST_ID].ToString();
        txtDescription.Text = detailMatHang[DRMConstants.DB_COLUMN_MAST_DESC].ToString();
        txtMaSo1.Text = detailMatHang[DRMConstants.DB_COLUMN_MAST_ID + "1"].ToString();
        txtDescription1.Text = detailMatHang[DRMConstants.DB_COLUMN_MAST_DESC + "1"].ToString();
        txtExtendedCode.Text = detailMatHang[DRMConstants.DB_COLUMN_MAST_EXTENDED_CODE].ToString();
        /* This info does not exist in select store procedure
        txtUnit.Text = detailMatHang[DRMConstants.DB_COLUMN_UNIT].ToString();
        */
        txtPrice.Text = detailMatHang[DRMConstants.DB_COLUMN_PRICE].ToString();
        txtPV.Text = detailMatHang[DRMConstants.DB_COLUMN_PV].ToString();
        txtBV.Text = detailMatHang[DRMConstants.DB_COLUMN_BV].ToString();

        /* This info does not exist in select store procedure
        ddlStatus.SelectedValue = detailMatHang[DRMConstants.DB_COLUMN_STATUS].ToString();
        ddlVAT.SelectedValue = detailMatHang[DRMConstants.DB_COLUMN_VAT].ToString();
        ddlProduct.SelectedValue = detailMatHang[DRMConstants.DB_COLUMN_PRODUCT].ToString();
        */
        ddlNganhHang.SelectedValue = detailMatHang[DRMConstants.DB_COLUMN_CATEGORY_ID].ToString();
        ddlNhanHieu.SelectedValue = detailMatHang[DRMConstants.DB_COLUMN_BRAND_ID].ToString();
    }

    private DataRow getDetailMatHang(string mastID)
    {
        dt = new ClassExecute.ClassExecute();

        DataTable db = dt.Get_Data_DataTable(str, ref error, DRMConstants.STOREPROC_NAME_GET_DETAIL_MAST, CommandType.StoredProcedure,
                        new SqlParameter(DRMConstants.STOREPROC_PARAM_MAST_ID, mastID));

        return db.Rows[0];
    }

    private void insertMatHang()
    {
        int result;
        result = dt.ExecuteSQl_int(str, DRMConstants.STOREPROC_NAME_SAVE_MAST, CommandType.StoredProcedure, ref error,
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_MAST_ID, txtMaSo.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_DESC, txtDescription.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_MAST_ID + "1", txtMaSo1.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_DESC + "1", txtDescription1.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_BARCODE, txtExtendedCode.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_UNIT, txtUnit.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_VAT_RATE, ddlVAT.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_CATEGORY_ID, ddlVAT.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_BRAND_ID, ddlNhanHieu.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_PRODUCT, ddlVAT.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_PRICE, DRMConstants.KEY_WORD_NEW),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_STATUS, ddlStatus.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW, 1));
        if (result == 0)
        {
            uscMsgBox1.AddMessage(String.Format(DRMConstants.MESSAGE_ADD_SUCCESSFULL, DRMConstants.OBJECT_NAME_MAST), uscMsgBox.enmMessageType.Error);
        }
        else
        {
            uscMsgBox1.AddMessage(DRMConstants.MESSAGE_UNDER_CONSTRUCTION, uscMsgBox.enmMessageType.Error);
        }
    }

    private void updateMatHang()
    {
        int result;
        result = dt.ExecuteSQl_int(str, DRMConstants.STOREPROC_NAME_SAVE_MAST, CommandType.StoredProcedure, ref error,
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_MAST_ID, txtMaSo.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_DESC, txtDescription.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_BARCODE, txtExtendedCode.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_UNIT, txtUnit.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_VAT_RATE, ddlVAT.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_CATEGORY_ID, ddlVAT.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_BRAND_ID, ddlNhanHieu.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_PRODUCT, ddlVAT.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_PRICE, DRMConstants.KEY_WORD_NEW),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_STATUS, ddlStatus.SelectedValue),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW, 0));
        if (result == 0)
        {
            uscMsgBox1.AddMessage(String.Format(DRMConstants.MESSAGE_ADD_SUCCESSFULL, DRMConstants.OBJECT_NAME_MAST), uscMsgBox.enmMessageType.Error);
        }
        else
        {
            uscMsgBox1.AddMessage(DRMConstants.MESSAGE_UNDER_CONSTRUCTION, uscMsgBox.enmMessageType.Error);
        }
    }

    private void LoadGrid()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, DRMConstants.STOREPROC_NAME_SEARCH_MASTS, CommandType.StoredProcedure,
                new SqlParameter(DRMConstants.STOREPROC_PARAM_CONTENT, txtSearch.Text.Trim()));
            grvMatHang.DataSource = db;
            grvMatHang.DataBind();
            if (db.Rows.Count == 0)
            {
                uscMsgBox1.AddMessage(DRMConstants.MESSAGE_NO_RECORD_FOUND, uscMsgBox.enmMessageType.Attention);
            }
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }

    private string getLogURL()
    {
        return Server.MapPath(webapp4U.Globals.LogUrl());
    }
    #endregion
}